from common.utils import blogDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#登录测试
class login:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = blogDriver.driver
        self.driver.get(self.url)

#正确用例
    #账号正确，密码正确
    def login_test01(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        text = WebDriverWait(self.driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, "body > div.container > div.left > div > h3")))

        blogDriver.screenshot()
        assert text.text == "zhangsan"


#错误用例
    #账号正确，密码错误
    def login_test02(self):
        self.driver.back()
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 等待 alert 出现，最多等 5 秒
        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        assert alert.text == "密码错误"
        alert.accept()

        blogDriver.screenshot()

    # 账号正确，密码为空
    def login_test03(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 等待 alert 出现，最多等 5 秒
        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        assert alert.text == "账号或密码不能为空"
        alert.accept()

        blogDriver.screenshot()

    #账号为空
    def login_test04(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 等待 alert 出现，最多等 5 秒
        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        assert alert.text == "账号或密码不能为空"
        alert.accept()

        blogDriver.screenshot()

    #账号错误
    def login_test05(self):
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("z")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 等待 alert 出现，最多等 5 秒
        alert = WebDriverWait(self.driver, 5).until(EC.alert_is_present())
        assert alert.text == "用户不存在"
        alert.accept()

        blogDriver.screenshot()
